package com.bbzn.device.client.service.emq.order.impl.mq;

import com.alibaba.fastjson.JSONObject;
import com.bbzn.device.client.constant.DeviceConstant;
import com.bbzn.device.client.dataobject.Device;
import com.bbzn.device.client.dataobject.SystemParam;
import com.bbzn.device.client.exception.DefinitException;
import com.bbzn.device.client.service.DeviceService;
import com.bbzn.device.client.service.SystemParamByCacheService;
import com.bbzn.device.client.utils.OptionalBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

/**
 * @Author wulongbo
 * @Date 2021/12/16 16:19
 * @Version 1.0
 */
@Component
public class MqSetReloadImpl extends BaseMqSendImpl {

    @Autowired
    private SystemParamByCacheService systemParamByCacheService;

    @Autowired
    private DeviceService deviceService;

    @Override
    public String checkMap(String language) throws Exception {
        if (!OptionalBean.ofNullable(map.get("interval")).isPresent()) {
            SystemParam systemParam = systemParamByCacheService.getValues(DeviceConstant.DEVICE_ENUM, DeviceConstant.DEVICE_UN_RELOAD_INTERVAL_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }

        Device device = new Device();
        device.setImei(map.get("imei").toString());
        device.setReloadInterval(Integer.parseInt(map.get("interval").toString()));
        // 设备重启时间设置状态【0：修改成功 / 1：修改中】
        device.setReloadState(1);
        // 1.修改设备重启时间
        deviceService.updateByImeiSelective(device);
        return JSONObject.toJSONString(map);
    }
}
